﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace childcare
{
    class connectdatabase
    {
        public static SqlConnection con = null;
        public static void Openconnect()
        {
            try
            {
                string connect = @"Data Source=BLACK-PC\SQLEXPRESS;Initial Catalog=Childcare;Integrated Security=True";
                con = new SqlConnection(connect);
                con.Open();
              //  MessageBox.Show("Kết nối thành công");
            }
            catch
            {
                MessageBox.Show("Kết nối không thành công");
            }
        }
        public static void closeconnect()//dong ket noi
        {
            try
            {
                string connect = @"Data Source=BLACK-PC\SQLEXPRESS;Initial Catalog=Childcare;Integrated Security=True";
                con = new SqlConnection(connect);
                con.Close();
            }
            catch
            { 
            
            }
        }
        public static DataTable ex(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public static void exc(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
        }
        public static DataTable laybang(string tenbang)// ham nay dung de lay bang
        {
            string sql = ("select * from") + tenbang;
            SqlCommand cmd = new SqlCommand(sql,con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlCommandBuilder cmb = new SqlCommandBuilder(da);
            DataSet ds = new DataSet();
            da.Fill(ds, tenbang);
            return ds.Tables[0];

        }
    }
}

